package com.caowei.weicimpress.weicimpress;

import android.app.Activity;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.caowei.weicimpress.weicimpress.widget.AlertDialog;

import java.util.List;
/**
 * Created by caoweiPc on 2014/7/25.
 * 生成各种风格一致的弹框
 */
public class GenerateDialog {
    private static final String TAG=GenerateDialog.class.getSimpleName();
    public static AlertDialog createInputDialog(String title,
                                                     final Activity activity,
                                                     final OnClickListener yesListener,
                                                     final OnClickListener noListener){
      /*  AlertDialog.Builder builder=new AlertDialog.Builder(activity);
        final AlertDialog dialog= builder.create();*/
        AlertDialog.Builder builder=new AlertDialog.Builder(activity,R.style.alertdialogCustom);
        View alertV=LayoutInflater.from(activity).inflate(R.layout.alertdialoginput,null);
        builder.setView(alertV);
        final AlertDialog dialog= builder.createDialog();
        dialog.setCanceledOnTouchOutside(false);
        ((TextView)alertV.findViewById(R.id.title)).setText(title);
        final EditText editText= (EditText) alertV.findViewById(R.id.edit);
        View close=alertV.findViewById(R.id.close);
        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(editText.getTag(R.id.edit).equals(true)){
                    editText.setText("");
                    editText.setHintTextColor(activity.getResources().getColor(R.color.gray_deep));
                    editText.setHint(activity.getString(R.string.writedirname));
                    editText.setTag(R.id.edit,false);
                }
            }
        });

        final CallbackDismiss callbackdismiss= new CallbackDismiss() {
            @Override
            public void onDismiss(boolean dismiss) {
                if(!dismiss){
                    editText.setText("");
                    editText.setHint(editText.getText().toString()+"已存在,请重新填写");
                    editText.setHintTextColor(activity.getResources().getColor(R.color.red_deep));
                    editText.setTag(R.id.edit,true);
                    return;
                }
                dialog.dismiss();
            }
        };


        Button btn1= (Button) alertV.findViewById(R.id.ok);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(TextUtils.isEmpty(editText.getText())){
                    editText.setHint("输入不可为空!");
                    editText.setHintTextColor(activity.getResources().getColor(R.color.red_deep));
                    editText.setTag(R.id.edit, true);
                    return;
                }
                v.setTag(editText.getText().toString());

                editText.setOnKeyListener(new View.OnKeyListener() {
                    @Override
                    public boolean onKey(View v, int keyCode, KeyEvent event) {
                        if((keyCode==KeyEvent.KEYCODE_DEL)&&(event.getAction()==
                                KeyEvent.ACTION_DOWN)){
                            String text=editText.getText().toString();
                            if(!TextUtils.isEmpty(text)&&text.length()==1){
                                if(editText.getTag(R.id.edit).equals(true)){
                                    editText.setText("");
                                    editText.setHintTextColor(activity.getResources().getColor(R.color.gray_deep));
                                    editText.setHint(activity.getString(R.string.writedirname));
                                    editText.setTag(R.id.edit,false);
                                    return true;
                                }
                            }
                        }
                        return false;
                    }
                });
                if(yesListener!=null){
                    yesListener.onClick(v,callbackdismiss);
                }else{
                    dialog.dismiss();
                }
            }
        });
        Button btn2= (Button) alertV.findViewById(R.id.cancel);
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(noListener!=null){
                    noListener.onClick(v,new CallbackDismiss() {
                        @Override
                        public void onDismiss(boolean dismiss) {
                            dialog.dismiss();
                        }
                    });
                }else{
                    dialog.dismiss();
                }
            }
        });
        dialog.setCanceledOnTouchOutside(false);
        return dialog;
    }
    /*列表弹框*/
    public static AlertDialog createSelectOperDialog(String title,
                                                     final Activity activity,
                                                     final List<ActionItem> actionItemses){
        AlertDialog.Builder builder=new AlertDialog.Builder(activity,R.style.alertdialogCustom);


        View alertV=LayoutInflater.from(activity).inflate(R.layout.alertdialog,null);
        builder.setView(alertV);
        final AlertDialog dialog= builder.createDialog();
        ((TextView)alertV.findViewById(R.id.title)).setText(title);
        ListView alertVL= (ListView) alertV.findViewById(R.id.list);
        final BaseAdapter adapter= new BaseAdapter() {
            @Override
            public int getCount() {
                return actionItemses.size();
            }

            @Override
            public ActionItem getItem(int position) {
                return actionItemses.get(position);
            }

            @Override
            public long getItemId(int position) {
                return actionItemses.get(position).hashCode();
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                if(convertView==null){
                    TextView v= (TextView) LayoutInflater.from(activity).
                            inflate(R.layout.alertdialog_item, parent, false);
                    v.setText(getItem(position).getName());
                    convertView=v;
                }
                return convertView;
            }
        };
        alertVL.setAdapter(adapter);
        alertVL.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ActionItem actionItem= (ActionItem) adapter.getItem(position);
                dialog.dismiss();
                if(actionItem.callback!=null){
                    actionItem.callback.callback();
                }
              //  exeDialogOper(dialog, position, bean);
            }
        });
        alertVL.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                ActionItem actionItem= (ActionItem) adapter.getItem(position);
                dialog.dismiss();
                if(actionItem.callback!=null){
                    actionItem.callback.callback();
                }
                //exeDialogOper(dialog, position,bean);
                return true;
            }
        });
        return dialog;
    }
    /*
      创建风格一致的确认对话框
      * */
    public static com.caowei.weicimpress.weicimpress.widget.AlertDialog  createVerifyDialog(
                                               Activity activity,
                                               String title,
                                               final View.OnClickListener yesListener,
                                               final View.OnClickListener noListener){
       /* View verify= LayoutInflater.from(activity).inflate(R.layout.verify_layout, null);
        AlertDialog.Builder builder=new AlertDialog.Builder(activity);
        builder.setView(verify);
        final AlertDialog dialog= builder.create();
        dialog.setCanceledOnTouchOutside(false);
        ((TextView)verify.findViewById(R.id.title)).setText(title);
        verify.findViewById(R.id.ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(yesListener!=null){
                    yesListener.onClick(v);
                }
                dialog.dismiss();
            }
        });
        verify.findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(noListener!=null){
                    noListener.onClick(v);
                }
                dialog.dismiss();
            }
        });*/
        View verify= LayoutInflater.from(activity).inflate(R.layout.verify_layout, null);
        com.caowei.weicimpress.weicimpress.widget.AlertDialog.Builder builder=
                new com.caowei.weicimpress.weicimpress.widget.AlertDialog.Builder(activity,
                        R.style.alertdialogCustom);
        builder.setView(verify);
        final com.caowei.weicimpress.weicimpress.widget.AlertDialog dialog= builder.createDialog();
        dialog.setCanceledOnTouchOutside(false);
        ((TextView)verify.findViewById(R.id.title)).setText(title);
        verify.findViewById(R.id.ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(yesListener!=null){
                    yesListener.onClick(v);
                }
                dialog.dismiss();
            }
        });
        verify.findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(noListener!=null){
                    noListener.onClick(v);
                }
                dialog.dismiss();
            }
        });
        return dialog;
    }

    public static AlertDialog createVerifyDialog(
            Activity activity,
            String title,
            BtnBean[] btnes ){
        View verify= LayoutInflater.from(activity).inflate(R.layout.verify_layout1, null);
        AlertDialog.Builder builder=new AlertDialog.Builder(activity,R.style.alertdialogCustom);
        LinearLayout btnlayout= (LinearLayout) verify.findViewById(R.id.btnlayout);

        builder.setView(verify);
        final AlertDialog dialog= builder.createDialog();
        for (final BtnBean btnBean:btnes ){
            Button button=new Button(activity);
            LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(0,
                    ViewGroup.LayoutParams.MATCH_PARENT,1);

            button.setLayoutParams(params);
            button.setGravity(Gravity.CENTER);
            button.setTextColor(activity.getResources().getColor(R.color.pure_white));
            button.setBackgroundResource(R.drawable.btn_bg);
            button.setTextSize(TypedValue.COMPLEX_UNIT_PX,
                    activity.getResources().getDimension(R.dimen._10sp));

            button.setText(btnBean.btnText);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                    if (btnBean.getListener() != null) {
                        btnBean.getListener().onClick(v);
                    }
                }
            });

            btnlayout.addView(button);
            TextView dividor=new TextView(activity);
            LinearLayout.LayoutParams params1=new LinearLayout.LayoutParams(4,
                    ViewGroup.LayoutParams.MATCH_PARENT);
            dividor.setLayoutParams(params1);
            btnlayout.addView(dividor);
        }
        ((TextView)verify.findViewById(R.id.title)).setText(title);
        dialog.setCanceledOnTouchOutside(false);
        return dialog;
    }

    public static class BtnBean{
        private String btnText;
        private View.OnClickListener listener;

        public String getBtnText() {
            return btnText;
        }

        public void setBtnText(String btnText) {
            this.btnText = btnText;
        }

        public View.OnClickListener getListener() {
            return listener;
        }

        public void setListener(View.OnClickListener listener) {
            this.listener = listener;
        }
    }
    public static class ActionItem{
        private String name;
        private Callback callback;
        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Callback getCallback() {
            return callback;
        }

        public void setCallback(Callback callback) {
            this.callback = callback;
        }
    }
    public static interface Callback<T>{
        public void callback(T... t);
    };

    public static interface CallbackDismiss{
        public void onDismiss(boolean dismiss);
    }

    public static interface OnClickListener{
        public void onClick(View v, CallbackDismiss callbackDismiss);
    }
}
